/**
 * 
 */
package com.tjyx.maintain.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tjyx.common.util.ProductClassUtil;
import com.tjyx.maintain.dao.NewsClassDao;
import com.tjyx.maintain.dao.ProductClassDao;
import com.tjyx.maintain.dto.ProductClass;

/**
 * @author liuyuxin
 *
 */
public class ProductClassAction {

	private static  ProductClassDao productClassDao=new  ProductClassDao();
	
	//查询产品类别和添加产品类别
	public String addProductClass(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
	 String parentClass=request.getParameter("parentClass");
	 String prodcutClassName=request.getParameter("prodcutClassName");
	 String sortnumber=request.getParameter("sortnumber");
	 if(parentClass!=null&&!parentClass.equals("")){
		 try {
			 productClassDao.addProductClass(Integer.valueOf(sortnumber), Integer.valueOf(parentClass), prodcutClassName);
			request.setAttribute("addMessage","添加成功！");
		} catch (SQLException e) {
			request.setAttribute("addMessage","添加失败！");
			e.printStackTrace();
		}
	 }
		 try {
			List<ProductClass> allproductClass=productClassDao.queryProductClass();
			List<ProductClass> firstDir=ProductClassUtil.getfirstDir(allproductClass);
			LinkedHashMap<Integer,List<ProductClass>> hashMap=ProductClassUtil.getAllChildernByParent(allproductClass);
			request.setAttribute("hashMapChilden",hashMap);
			request.setAttribute("firstDir",firstDir);
			request.setAttribute("allproductClass",allproductClass);
			request.setAttribute("queryMessage","查询成功！");
		} catch (SQLException e) {
			request.setAttribute("queryMessage","查询失败！");
			e.printStackTrace();
		}
	return "maintain/product/buildUpProductClass.jsp";
	}
	

	public String deleteProductClass(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		 String id=request.getParameter("id");
		 int idDelete=0;
		 if(id!=null&&!id.equals("")){
			 idDelete=Integer.valueOf(id);
		 }
		 try {
			 productClassDao.deleteProductClass(idDelete);
		  	request.setAttribute("delete","删除成功！");
		} catch (SQLException e) {
			request.setAttribute("delete","删除失败！");
			e.printStackTrace();
		}
		PrintWriter pw=response.getWriter();
		pw.print(request.getAttribute("delete").toString());
		return "NoResult";
	}
	
	public String updateProductClass(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String id=request.getParameter("id");
		String columnValue=null;
		String columnName=null;
		if(request.getParameter("sortNumber")!=null){
			columnValue=request.getParameter("sortNumber");
			columnValue=new   String(columnValue.getBytes("iso-8859-1"),"utf-8");
			columnName="sortNumber";
		}else if(request.getParameter("productClassName")!=null){
			columnValue=request.getParameter("productClassName");
			columnValue=new   String(columnValue.getBytes("iso-8859-1"),"utf-8");
			columnName="productClassName";
		}
		System.out.println("id columnName columnValue  "+id+"  "+columnName+" "+columnValue);
		 try {
			 productClassDao.updateProductClass(Integer.valueOf(id), columnName,columnValue);
			request.setAttribute("update","修改成功！");
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			request.setAttribute("update","修改失败！");
			e.printStackTrace();
		}
		PrintWriter pw=response.getWriter();
		pw.print(request.getAttribute("update").toString());
		return "NoResult";
	}
}
